import easytrader
from utils.log_util import prints
import datetime
import threading
import time


class AutoTradeWorker():
    """
    股票自动交易
    """

    accounnt = 880007077431
    pwd = 110132
    exePath = r'D:\tonghuashun\xiadan.exe'

    timeSleep = 1

    def __init__(self):
        self.user = easytrader.use('universal_client')
        self.user.prepare(user=self.accounnt,
                          password=self.pwd,
                          exe_path=self.exePath)

    def startThread(self):
        thread = threading.Thread(target=self.execution)
        thread.start()

    def execution(self):

        prints("股票自动交易服务已启动...")

        while True:
            nowTime = datetime.datetime.now()
            weekday = nowTime.weekday()
            hourNow = nowTime.hour
            minuteNow = nowTime.minute

            if weekday < 5:
                if hourNow > 9 and hourNow < 12 or hourNow > 12 and hourNow < 16:
                    accountInfo = self.user.balance
                    # position = self.user.position
                    print("资金余额：" + str(accountInfo["资金余额"]) + " 可用余额：" +
                          str(accountInfo["可用金额"]) + " 可取金额：" +
                          str(accountInfo["可取金额"]) + " 总资产：" +
                          str(accountInfo["总资产"]))
            time.sleep(self.timeSleep)